package com.haochu.novel.ui;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testnovel.R;
import com.haochu.novel.db.DBHelper;
import com.haochu.novel.util.Constants;
import com.haochu.novel.vo.Book;

/**
 * @author fuhuichao
 * 2017年6月27日14:41:23
 * 小说列表
 */
public class BookListActivity extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener{

	private Toast toast;
	private DBHelper dbHelper;
	private List<Book> books;
	private MyAdapter myAdapter;
	
	// 导入按钮
	private Button btn_import;
	// 书架列表
	private ListView lv_books;
	
	
	@SuppressLint("ShowToast") 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_book_list);
		
		// 初始化吐司
		toast = Toast.makeText(BookListActivity.this, "", Toast.LENGTH_SHORT); 
		
		// 获取书架所有小说
		dbHelper = DBHelper.getInstance(this);
//		dbHelper.deleteBooks();
		books = dbHelper.listBook();
		
		// 书架列表
		myAdapter = new MyAdapter(this);
		lv_books = (ListView) findViewById(R.id.lv_books);
		lv_books.setAdapter(myAdapter);
		lv_books.setOnItemClickListener(this);

		// 导入按钮
		btn_import = (Button) findViewById(R.id.btn_import);
		btn_import.setOnClickListener(this);
		
	}

	/**
	 * 单击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_import:
			Intent intent = new Intent(BookListActivity.this, BookDirectoryActivity.class);
			startActivityForResult(intent, Constants.REQUEST_CODE_ACTIVITY);
			break;
		default:
			break;
		}
	}
	

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

		return true;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//		String path = "/mnt/sdcard/test.txt";
//		String path = "/storage/emulated/0/test.txt";
		Book book = books.get(position);
		Intent intent = new Intent(BookListActivity.this, BookContentActivity.class);
		intent.putExtra("bookId", book.getId());
		startActivityForResult(intent, Constants.REQUEST_CODE_ACTIVITY);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == Constants.REQUEST_CODE_ACTIVITY){
			if(resultCode == Constants.RESULT_CODE_DIRECTORY){
				books = dbHelper.listBook();
				myAdapter.notifyDataSetChanged();
			}
		}
	}
	
	/**
	 * 自定义adapter
	 */
	class MyAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		public MyAdapter(Context context){
			this.mInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return books.size();
		}

		@Override
		public Object getItem(int position) {
			return books.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams") 
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
			}
			TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
			textView.setText(books.get(position).getName());
			
			return convertView;
		}
		
	}
	
	/**
	 * 打土司
	 */
	public void showTip(String text){
		toast.setText(text);
		toast.show();
	}
	
}
